04. Stack and Heap
C++ Memory MGMT 01 Introduction A02 Stack And Heap
Stack and Heap
Description
In general, memory management in C++ can be split into two major categories by their storage in the host machine. When we analyze this classification, we have two main concepts: stack and heap.
Comparison
The Stack
- only local variables
- resizing of variables cannot be done
- efficiently managed space by CPU, memory does not become fragmented
- very quick access
- there is a limit on stack size (OS-dependent)
- explicit de-allocation of variables is not mandatory
The Heap
- memory must be managed
- memory size is not limited
- access is relatively slower
- realloc() is used for the resizing of variables
- variables can be accessed globally
- efficient use of space is not guaranteed, memory can become fragmented over time when blocks of memory are allocated, then freed